<?php
require_once "db.php";
session_start();
$movie_name = db_escape($_POST['moviename']);

$query_text = "SELECT * FROM movies WHERE movie_name = '$movie_name'";
$query = db_query($query_text);
$row = mysqli_fetch_assoc($query);
$id = $row['idmovies'];
$duration = $row['duration'];
$picture = $row['picture_url'];
$genre1 = $row['m_genre1'];
$genre2 = $row['m_genre2'];
$genre3 = $row['m_genre3'];
$rating = $row['overal_rating'];
$description = $row['description'];
$addDescription ="<div id='movietitle'><span id='movietitle_span'>".$movie_name."</span></div>
<div id='movie_cover'>
	<img src='movie-covers".$picture."'/></div>
	<div class='other_spec'><span id='duration_span'>Duration: ".$duration." minutes</span></div>
	<div class='other_spec'><span id='genre_span'>Genre: ".$genre1;
if($genre2!=$genre1){
	$addDescription .= ", ".$genre2;
}
if ($genre3!=$genre1 && $genre3!=$genre2) {
	$addDescription .= ", ".$genre3;
}
$addDescription .="</span></div><div class='other_spec'><span id='rating_span'>Rating: ";
if(is_null($rating)){
	$addDescription .= "Not rated yet</span></div>";
}
else {
	$addDescription .= $rating."</span></div>";
}
if ($row['past_present_future']==1 || $row['past_present_future']==4) {
	$addDescription .= "<div class='other_spec'><span id='available_span'> Can be seen in: ";
	$availability_query_text = "SELECT cinema_idcinema FROM cinema_has_movies WHERE movies_idmovies='$id'";
	$availability_query = db_query($availability_query_text);
	while ($row = mysqli_fetch_assoc($availability_query)) {
		$cin_id = $row['cinema_idcinema'];
		$get_cin_query_text = "SELECT cinema_name FROM cinema WHERE idcinema='$cin_id'";
		$get_cin_query = db_query($get_cin_query_text);
		$row_cin = mysqli_fetch_assoc($get_cin_query);
		$addDescription .= $row_cin['cinema_name']." ";
	}
	$addDescription .="</span></div>";
}
$addDescription .= "<div id='description'><span id='descr_span'>".$description."</span></div>
					<div id='read-comment' data-movieid='".$id."'><a href='#'>Read commens</a></div>";

if( isset($_SESSION['uid']) ){
	$addDescription .= "<div id='show-proj' data-movieid='".$id."'><a href='#'>Show projections</a></div>";
}
echo $addDescription;
